﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;

//**************************************************************************************************
// 系统名称	：沿海集团任务管理系统
// 文件名称	： SessionBox.cs
// 做成履历 ：
//      日期                    姓名            内容
//      2015-4-25       XXXXXXXXX           Session会话公共类（读取，创建，删除）
//**************************************************************************************************
namespace CRM.UTIL
{
    /// <summary>
    /// 用于读、写、删除、比较Session中的用户信息。
    /// </summary>
    [Serializable]
    public class SessionBox
    {

        static string url = "Http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port;

        private SessionBox() { }

        #region 用户Session操作

        /// <summary>
        /// 检测User Session是否存在
        /// </summary>
        /// <returns>每个页面</returns>
        public static bool CheckUserSession()
        {
            object o = System.Web.HttpContext.Current.Session["USER"];
            if (o == null)
            {
                StringBuilder str = new StringBuilder();

                str.Append("<script   language='javascript'>");
                str.Append("alert('非法登陆或连接超时，请重新登录！');");
                str.Append("if(window.opener != null)\n");
                str.Append("{\n");
                str.Append("window.opener.location.replace(window.opener.location);\n");
                str.Append("}\n");
                str.Append("parent.location='" + url + "/" + System.Configuration.ConfigurationManager.AppSettings["LoginPage"] + "';");
                str.Append("top.location='" + url + "/" + System.Configuration.ConfigurationManager.AppSettings["LoginPage"] + "';");
                str.Append("parent.document.location.href='" + url + "/" + System.Configuration.ConfigurationManager.AppSettings["LoginPage"] + "';");
                str.Append("</script>");

                //获取请求页面的URL
                string returnURl = HttpContext.Current.Request.RawUrl;

                HttpContext.Current.Response.Write(str.ToString());
                string path = url + "/" + System.Configuration.ConfigurationManager.AppSettings["LoginPage"] + "?ReturnUrl=" + returnURl;
                HttpContext.Current.Response.Redirect(path);

                HttpContext.Current.Response.End();
                return false;
            }
            else
            {
                return true;
            }
          //  return true;
        }

        /// <summary>
        /// 登记User Session
        /// </summary>
        /// <param name="userinfo"></param>
        public static void CreateUserSession(UserSession userinfo)
        {
            System.Web.HttpContext.Current.Session["USER"] = userinfo;
        }

        /// <summary>
        /// 获取User Session
        /// </summary>
        /// <returns></returns>
        public static UserSession GetUserSession()
        {
            object o = System.Web.HttpContext.Current.Session["USER"];
            if (o == null)
            {
                HttpContext.Current.Response.Redirect(url + "/" + System.Configuration.ConfigurationManager.AppSettings["LoginPage"] + "");
                return null;
            }

                //throw new Exception("读取用户权限失败。");
            else return (o as UserSession);
        }

        /// <summary>
        /// 移除User Session
        /// </summary>
        public static void RemoveUserSession()
        {
            object o = System.Web.HttpContext.Current.Session["USER"];
            if (o != null) System.Web.HttpContext.Current.Session.Remove("User");
        }

        #endregion

        #region 当前已登录会员对当前页面的权限集合

        /// <summary>
        /// 创建页面权限列表
        /// </summary>
        /// <param name="lists"></param>
        public static void CreatePageAuthority(ArrayList lists)
        {
            System.Web.HttpContext.Current.Session["PageAuthority"] = lists;
        }

        /// <summary>
        /// 读取页面权限
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetPageAuthority()
        {
            object o = System.Web.HttpContext.Current.Session["PageAuthority"];
            if (o == null) throw new Exception("读取页面权限失败。");
            else return (o as ArrayList);
        }

        /// <summary>
        /// 移除页面权限
        /// </summary>
        public static void RemovePageAuthority()
        {
            object o = System.Web.HttpContext.Current.Session["PageAuthority"];
            if (o != null) System.Web.HttpContext.Current.Session.Remove("PageAuthority");
        }

        #endregion
    }

}
